package net.fltx.p2p.client;

import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.OverrunStyle;
import javafx.scene.shape.Circle;
import net.fltx.p2p.common.P2PClientInfo;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;

public class P2PClientInfoCell extends ListCell<P2PClientInfo>   {
    private Node cellNode;

    private P2PClientInfo value;
    public P2PClientInfo getValue() {
        return value;
    }

    public void setValue(P2PClientInfo value) {
        this.value = value;
    }

    public P2PClientInfoCell(){
        try {
            URL url = getClass().getResource("/P2PClientInfoCell.fxml");
            cellNode = FXMLLoader.load(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void updateItem(P2PClientInfo item, boolean empty) {
        super.updateItem(item, empty);
        if(empty || item==null){
            setText(null);
            return;
        }else {
            if(cellNode!=null){
                Circle circle= (Circle) cellNode.lookup("#status");
                Label username= (Label) cellNode.lookup("#username");
                Label address= (Label) cellNode.lookup("#address");
                username.setText(item.getUsername());
                address.setText(item.getAddress());
                cellNode.prefWidth(getParent().getBoundsInLocal().getWidth());
                setGraphic(cellNode);

            }else {
                setText(item.getUsername()+"@"+item.getUuid());
            }

        }
    }
}
